<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/logout.js"></script>
    <script src="js/urltools.js"></script>


    <style>
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>

</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="javascript:logout()">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">

        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <div id="artlist">

            </div>
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <div class="blog" >
            </div>
            <div id="pageDiv" class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="toFirst()">首页</button>
                <button class="blog-pagnation-item" onclick="toPre()">上一页</button>
                <button class="blog-pagnation-item" onclick="toNext()">下一页</button>
                <button class="blog-pagnation-item" onclick="toLast()">末页</button>
            </div>
        </div>
    </div>
</body>
<script>
    var pageIndex = 1; // 当前页码，默认从1开始
    var pageSize = 4;  // 每页显示条数，默认为4
    var pageCount = 0; // 总页数
    // 页面初始化
    function pageInit() {
        var paramPageIndex = getParamByKey("pageIndex");
        var paramPageSize = getParamByKey("pageSize");
        if(paramPageIndex!=null){
            pageIndex = paramPageIndex;
        }
        if(paramPageSize!=null){
            pageSize = paramPageSize;
        }
        getMyArtList();
    }

    pageInit();

    // 点击首页
    function toFirst() {
        if(pageIndex <= 1){
            alert("抱歉：当前已经在首页，不进行跳转！");
            return false;
        }
        pageIndex = 1;
        location.href = "myblog_list.html?pageIndex="+pageIndex+"&pageSize="+pageSize;

    }

    // 点击上一页
    function toPre() {
        if(pageIndex <= 1){
            alert("抱歉：当前在首页，无法进行跳转上一页操作！");
            return false;
        }
        pageIndex = pageIndex - 1;
        location.href = "myblog_list.html?pageIndex="+pageIndex+"&pageSize="+pageSize;
    }

    // 点击下一页
    function toNext() {
        if(pageIndex >= pageCount){
            alert("抱歉：当前在末页，无法进行跳转下一页操作！");
            return false;
        }
        pageIndex = Number(pageIndex) + 1;
        location.href = "myblog_list.html?pageIndex="+pageIndex+"&pageSize="+pageSize;
    }

    // 点击末页
    function toLast() {
        if(pageIndex >= pageCount){
            alert("抱歉：当前已经在末页，无法进行跳末页操作！");
            return false;
        }
        pageIndex = pageCount;
        location.href = "myblog_list.html?pageIndex="+pageIndex+"&pageSize="+pageSize;
    }

    function getMyArtList(){
        jQuery.ajax({
            url:"/art/mylistbypage",
            type:"post",
            data:{
                "pageIndex":pageIndex,
                "pageSize":pageSize
            },
            success: function(res) {
                if(res.code==200 && res.data.length>0){
                    // 博客列表页
                    // for 循环拼接 div ，最终放到 artlist 里
                    var arthtml = "";
                    for(var i = 0; i < res.data.length; i++){
                        var art = res.data[i];
                        arthtml += '<div class="blog">';
                        arthtml += '<div class="title">'+ art.title+'</div>';
                        arthtml += '<div class="date">'+ art.createtime+'</div>';
                        arthtml += '<div class="desc">'+ art.content+'</div>';
                        arthtml += '<a href="blog_content.html?id='+art.id+
                            '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp';
                        arthtml += '<a href="blog_edit.html?id='+art.id+
                            '" class="detail">修改文章 &gt;&gt;</a>&nbsp;&nbsp';
                        arthtml += '<a href="javascript:delArt('+art.id+
                            ')" class="detail">删除文章 &gt;&gt;</a>&nbsp;&nbsp';
                        arthtml += '</div>';
                    }
                    jQuery("#artlist").html(arthtml)
                    jQuery("#pageDiv").show();
                }else{
                    jQuery("#pageDiv").hide();
                    jQuery("#artlist").html("<h2 style='margin:120px 120px;'> 该博客列表页暂无文章 </h2>")
                }
            }
        });

        // 2，查询文章的总数，并得出页数
        jQuery.ajax({
            url:"/art/getcountbyuser",
            type:"post",
            data:{},
            success:function(res){
                if(res.code==200&&res.data>0){
                    // 计算总页数
                    pageCount = Math.ceil(res.data/pageSize);
                }else{

                }
            }
        });
    }



    function delArt(id) {
        if(confirm("确认是否删除？")){
            // 确认删除
            jQuery.ajax({
                url:"art/del",
                type:"post",
                data:{
                    "id":id
                },
                success:function(res){
                    if(res.code==200 && res.data==1){
                        alert("恭喜：文章删除成功！");
                        location.href = location;
                    }else{
                        alert("抱歉：文章删除失败！"+res.message);
                    }
                }
            });
        }
    }


</script>

</html>